//
//  BLTimeAxisAppearanceRecgnizer.m
//  BLTimeAxis
//
//  Created by dg on 2025/4/18.
//

#import "BLTimeAxisAppearanceRecognizer.h"
#import "BLTimeAxisData.h"
#import "BLTimeAxisRule.h"
#import "BLTimeAxisDigitalDivision.h"
#import "BLTimeAxisBaseLine.h"
#import "BLTimeAxisBackground.h"

/// 整个视图的宽度
@interface BLTimeAxisAppearanceRecognizer()

@property (nonatomic, strong) BLTimeAxisAppearance *appearance;

@property (nonatomic, assign) CGFloat viewWidth;
@property (nonatomic, assign) CGFloat viewHeight;

@end

@implementation BLTimeAxisAppearanceRecognizer

- (instancetype)initWithViewSize:(CGSize)viewSize {
    if (self = [super init]) {
        [self setViewSize:viewSize];
    }
    return self;
}

- (instancetype)initWithViewSize:(CGSize)viewSize appearance:(BLTimeAxisAppearance *)appearance {
    if (self = [self initWithViewSize:viewSize]) {
        [self setAppearance:appearance];
        
        self.viewWidth = viewSize.width;
        self.viewHeight = viewSize.height;
    }
    return self;
}


- (void)visitTimeAxis:(id<BLTimeAxisComponent>)aTimeAxis {

}

- (void)visitTimeAxisData:(BLTimeAxisData *)aTimeAxisData {
    switch (self.appearance.dataStrokeSizeType) {
        case BLStrokeSizeTypeFull:
            aTimeAxisData.strokeSize = self.appearance.direction == BLAxisDirectionHorizontal ? self.viewHeight : self.viewWidth;
            break;
        case BLStrokeSizeTypeHalf:
            aTimeAxisData.strokeSize = (self.appearance.direction == BLAxisDirectionHorizontal ? self.viewHeight : self.viewWidth) / 2.0;
            break;
        default:
            aTimeAxisData.strokeSize = self.appearance.dataStrokeSize;
            break;
    }
}

- (void)visitTimeAxisRule:(BLTimeAxisRule *)aTimeAxisRule {
    aTimeAxisRule.axisDirection = (NSUInteger)self.appearance.direction;
    aTimeAxisRule.strokeColor = self.appearance.ruleColor;
    aTimeAxisRule.strokeSize = self.appearance.ruleStrokeSize;
    switch (self.appearance.ruleOffsetLocationType) {
        case BLStrokeLocationTypeFlexible:
            break;
        case BLStrokeLocationTypeMiddle:
            self.appearance.ruleFixedOffset = (self.appearance.direction == BLAxisDirectionHorizontal ? self.viewWidth : self.viewHeight) / 2.0;
            break;
        case BLStrokeLocationTypeMaximum:
            self.appearance.ruleFixedOffset = self.appearance.direction == BLAxisDirectionHorizontal ? self.viewWidth : self.viewHeight;
            break;
        case BLStrokeLocationTypeMinimum:
            self.appearance.ruleFixedOffset = 0.0;
            break;
        default:
            break;
    }
    aTimeAxisRule.fixedOffset = self.appearance.ruleFixedOffset;
}

- (void)visitTimeAxisBaseLine:(BLTimeAxisBaseLine *)aTimeAxisBaseLine {
    aTimeAxisBaseLine.strokeColor = self.appearance.baseLineColor;
    aTimeAxisBaseLine.strokeSize = self.appearance.baseLineStrokeSize;
    aTimeAxisBaseLine.minimumScale = self.appearance.minimumScale;
    aTimeAxisBaseLine.maximumScale = self.appearance.maximumScale;
    aTimeAxisBaseLine.oneToOneScaleMatchMaxHoursInVisible = self.appearance.oneToOneScaleMatchMaxHoursInVisible;
    
    switch (self.appearance.baseLineOffsetLocationType) {
        case BLStrokeLocationTypeFlexible:
            break;
        case BLStrokeLocationTypeMiddle:
            self.appearance.baseLineFixedOffset = (self.appearance.direction == BLAxisDirectionHorizontal ? self.viewHeight : self.viewWidth)/2.0;
            break;
        case BLStrokeLocationTypeMaximum:
            self.appearance.baseLineFixedOffset = self.appearance.direction == BLAxisDirectionHorizontal ? self.viewHeight : self.viewWidth;
            break;
        case BLStrokeLocationTypeMinimum:
            self.appearance.baseLineFixedOffset = 0.0;
            break;
        default:
            break;
    }
    aTimeAxisBaseLine.fixedOffset = self.appearance.baseLineFixedOffset;
}


- (void)visitTimeAxisDigitalDivision:(BLTimeAxisDigitalDivision *)aTimeAxisDigitalDivision {
    aTimeAxisDigitalDivision.strokeColor = self.appearance.divisionColor;
    aTimeAxisDigitalDivision.strokeSize = self.appearance.divisionStrokeSize;
    aTimeAxisDigitalDivision.digitalAttribute = self.appearance.digitalAttribute;
    aTimeAxisDigitalDivision.minimumScale = self.appearance.minimumScale;
    aTimeAxisDigitalDivision.maximumScale = self.appearance.maximumScale;
    aTimeAxisDigitalDivision.oneToOneScaleMatchMaxHoursInVisible = self.appearance.oneToOneScaleMatchMaxHoursInVisible;
}


- (void)visitTimeAxisBackground:(BLTimeAxisBackground *)aTimeAxisBackground {
    aTimeAxisBackground.strokeColor = self.appearance.backgroundColor;
    switch (self.appearance.backgroundStrokeSizeType) {
        case BLStrokeSizeTypeFull:
            aTimeAxisBackground.strokeSize = self.appearance.direction == BLAxisDirectionHorizontal ? self.viewHeight : self.viewWidth;
            break;
        case BLStrokeSizeTypeHalf:
            aTimeAxisBackground.strokeSize = (self.appearance.direction == BLAxisDirectionHorizontal ? self.viewHeight : self.viewWidth) / 2.0;
            break;
        default:
            aTimeAxisBackground.strokeSize = self.appearance.backgroundStrokeSize;
            break;
    }
}

- (BLTimeAxisAppearance *)appearance {
    if (!_appearance) {
        _appearance = [BLTimeAxisAppearance sharedAppearance];
    }
    return _appearance;
}

@end
